package com.github.caijh.mall.express.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.caijh.mall.common.CommonPage;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.express.model.Express;
import com.github.caijh.mall.express.request.ExpressSearchRequest;
import com.github.caijh.mall.express.request.ExpressUpdateRequest;
import com.github.caijh.mall.express.request.ExpressUpdateShowRequest;
import com.github.caijh.mall.express.service.ExpressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 快递公司表 前端控制器
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/express")
@Api(tags = "设置 -- 物流 -- 公司")
public class ExpressController {

    @Autowired
    private ExpressService expressService;

    /**
     * 分页显示快递公司表
     *
     * @param request    ExpressSearchRequest 搜索条件
     * @param pageParams 分页参数
     */
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParam(name = "keywords", value = "搜索关键字")
    public CommonResult<CommonPage<Express>> getList(@Validated ExpressSearchRequest request,
                                                     @ModelAttribute PageParams pageParams) {
        CommonPage<Express> expressCommonPage = CommonPage.restPage(expressService.getList(request, pageParams));
        return CommonResult.success(expressCommonPage);
    }

    /**
     * 编辑快递公司
     */
    @ApiOperation(value = "编辑")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated ExpressUpdateRequest expressRequest) {
        if (expressService.updateExpress(expressRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改显示状态
     */
    @ApiOperation(value = "修改显示状态")
    @RequestMapping(value = "/update/show", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated ExpressUpdateShowRequest expressRequest) {
        if (expressService.updateExpressShow(expressRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 同步物流公司
     */
    @ApiOperation(value = "同步物流公司")
    @RequestMapping(value = "/sync/express", method = RequestMethod.POST)
    public CommonResult<String> syncExpress() {
        if (expressService.syncExpress()) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }


    /**
     * 查询快递公司表信息
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-17
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "快递ID")
    public CommonResult<Express> info(@RequestParam(value = "id") Integer id) {
        Express express = expressService.getById(id);
        return CommonResult.success(express);
    }

    /**
     * 查询全部物流公司
     */
    @ApiOperation(value = "查询全部物流公司")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public CommonResult<List<Express>> all() {
        return CommonResult.success(expressService.findAll());
    }

    /**
     * 查询物流公司面单模板
     *
     * @return
     */
    @ApiOperation(value = "查询物流公司面单模板")
    @RequestMapping(value = "/template", method = RequestMethod.GET)
    @ApiImplicitParam(name = "com", value = "快递公司编号")
    public CommonResult<JSONObject> template(@RequestParam(value = "com") String com) {
        return CommonResult.success(expressService.template(com));
    }

}



